package sunwin.yois.web.common.controllerAspect;

import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import sunwin.yois.web.common.*;
import sunwin.yoms.domain.system.BlogrollDomain;
import sunwin.yoms.service.security.YomsUserDetails;
import sunwin.yoms.service.system.BlogrollService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by liangzizheng on 2015/8/31.
 */
@Component
public class YoisShareDataAspect {
    @Autowired
    private R r;
    @Autowired
    private Rx rx;
    @Autowired
    private YoisVelocityTools tools;
    @Autowired
    private YoisControllerDefaultData defaultData;
    @Autowired
    private BlogrollService blogrollService;
    @Autowired
    private WebConfig webConfig;

    public void addData(ModelAndView mav, HttpServletRequest request) {
        if(mav != null){
            mav.addObject("r", r);
            mav.addObject("rx", rx);
            mav.addObject("tools", tools);
            mav.addObject("webConfig", webConfig);
            mav.addObject("nickname", getNickname());
            mav.addObject("isLogin", isLogin());

            getCsrfToken(mav.getModelMap(), request);
            getFriendlyLink(mav.getModelMap());
        }
    }

    /**
     * 设置token
     * @param modelMap
     * @param request
     */
    private void getCsrfToken(ModelMap modelMap, HttpServletRequest request){
        CsrfToken csrfToken = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
        if (csrfToken != null) {
            modelMap.addAttribute("_csrf", csrfToken);
        }
    }

    /**
     * 获取友情链接
     *
     * @param modelMap
     */
    private void getFriendlyLink(ModelMap modelMap) {
        List<BlogrollDomain> friendlyLinks1 = null;
        List<BlogrollDomain> friendlyLinks2 = null;

        friendlyLinks1 = blogrollService.getBlogrollByPage(1, 10);
        friendlyLinks2 = blogrollService.getBlogrollByPage(2, 10);

        modelMap.addAttribute("friendlyLinks1", friendlyLinks1);
        modelMap.addAttribute("friendlyLinks2", friendlyLinks2);
    }

    /**
     * 获取用户是否登录
     *
     * @return
     */
    private boolean isLogin() {
        return defaultData.getCurrentYimsUserDetails() != null;
    }

    /**
     * 获取用户昵称
     *
     * @return
     */
    private String getNickname() {
        YomsUserDetails details = defaultData.getCurrentYimsUserDetails();
        if (details != null) {
            return StringUtil.isBlank(details.getNickname()) ? "您还没有昵称" : details.getNickname();
        }
        return null;
    }

    /**
     * 设置默认的videName
     * @param handler
     * @param modelAndView
     */
    public void setDefaultVelocityViewName(HandlerMethod handler, ModelAndView modelAndView) {
        //获取requestMapping注解
        RequestMapping mapping = handler.getMethodAnnotation(RequestMapping.class);

        if (modelAndView != null) {
            String vmName = modelAndView.getViewName();
            String folderName = mapping.name();
            //拼装vm文件路径
            String viewNameHtml = "vm/" + folderName + vmName;
            //将vm文件路径保存到modelAndView中
            modelAndView.setViewName(viewNameHtml);
        }
    }
}
